from curl_cffi import requests
import re
from util.logger import logger

logger = logger('static/彼岸图网', name=__file__.split('/')[-1].split('.')[1])
logger.info('开爬.......')
for page in range(1, 11):
    if page == 1:
        url = 'https://pic.netbian.com'
    else:
        url = f'https://pic.netbian.com/index_{page}.html'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
    }
    try:
        response = requests.get(url, headers=headers)
        logger.info(f'第{page}页开始')
        html = response.content.decode('gbk')
        results = re.findall(
            r'<li><a href="(/tupian.*?html)".*?target="_blank">.*?<img src="(.*?)" alt="(.*?)" />.*?<b>.*?</b></a></li>',
            html, re.S)
        for result in results:
            img_detail = 'https://pic.netbian.com' + result[0]
            title = result[2]
            suo_img_url = 'https://pic.netbian.com' + result[1]
            logger.info(f'解析{title}详情')
            detail_response = requests.get(img_detail, headers=headers)
            html = detail_response.content.decode('gbk')
            img_urls = re.findall(
                '<div class="photo-pic"><a href="" id="img"><img src="(.*?)" data-pic=".*?" alt=".*?" title=".*?"></a></div>',
                html, re.S)
            img_url = 'https://pic.netbian.com' + img_urls[0]
            headers2 = {
                'referer': img_detail,
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
            }
            img_response = requests.get(img_url, headers=headers2)
            headers3 = {
                'referer': url,
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
            }
            suo_img_response = requests.get(suo_img_url, headers=headers3)
            with open(f'static/彼岸图网/img/{title}_suo.jpg', 'wb') as f:
                f.write(suo_img_response.content)
            logger.info(f'{title}缩略图爬取成功')

            with open(f'static/彼岸图网/img/{title}_da.jpg', 'wb') as f:
                f.write(img_response.content)
            logger.info(f'{title}大图爬取成功')
    except Exception as e:
        logger.info(e)

logger.info(f'over.........')
